package com.kehao.leetcode.middle.other;

import org.junit.Test;

import java.util.Stack;

public class evalRPNSolution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> intStack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if(tokens[i].equals("+")){
                int num2 = intStack.pop();
                int num1 = intStack.pop();
                intStack.push(num1+num2);
            }else if(tokens[i].equals("-")){
                int num2 = intStack.pop();
                int num1 = intStack.pop();
                intStack.push(num1-num2);
            }else if(tokens[i].equals("*")){
                int num2 = intStack.pop();
                int num1 = intStack.pop();
                intStack.push(num1*num2);
            }else if(tokens[i].equals("/")){
                int num2 = intStack.pop();
                int num1 = intStack.pop();
                intStack.push(num1/num2);
            }else {
                intStack.push(Integer.parseInt(tokens[i]));
            }
        }
        return intStack.pop();
    }

    @Test
    public void test01(){
        String[] tokens = {"2","1","+","3","*"};
        System.out.println(evalRPN(tokens));
    }

}
